/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

package team.mai.antexpress.spring.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import team.mai.antexpress.dao.DesignationDAO;
import team.mai.antexpress.dao.UserDAO;
import team.mai.antexpress.entities.DesignationEntity;
import team.mai.antexpress.services.DepartmentService;
import team.mai.antexpress.services.DesignationService;
import team.mai.antexpress.services.UserService;
import team.mai.antexpress.spring.beans.LoginBean;

import java.util.List;

import static team.mai.antexpress.services.DesignationService.*;

@SuppressWarnings("Duplicates")
@Controller
public class DesignationController {
	@Autowired
	DesignationService designationService;
	@Autowired
	LoginBean login;
	@Autowired
	UserService userService;
	@Autowired
	DesignationDAO designationDAO;
	@Autowired
	UserDAO userDAO;

	@RequestMapping(value = "/admin/manage_designations", method = RequestMethod.GET)
	public String doGet(ModelMap map) {
		List<DesignationEntity> allDepartments = designationService.getAllDesignations(login.getUsername());
		map.addAttribute("dsgns", allDepartments);
		map.addAttribute("permissions", userDAO.getPermissionByUser(login.getUsername()));
		return "admin/manage_designations";
	}

	@RequestMapping(value = "/admin/new_designation", method = RequestMethod.GET)
	public String newDeptPage() {
		return "admin/new_designation";
	}

	@RequestMapping(value = "/admin/new_designation", method = RequestMethod.POST)
	@ResponseBody
	public String insertDept(DesignationEntity dsgn) {
		int id = designationService.insertDesignation(login.getUsername(), dsgn);
		if (id > 0) {
			return "{\"status\":\"success\", \"id\":" + id + "}";
		} else {
			StringBuilder stringBuilder = new StringBuilder("{\"status\":\"error\", \"error\":");
			stringBuilder.append("\"");
			if (id == DepartmentService.ERROR)
				stringBuilder.append("Internal Error");
			else if (id == DepartmentService.INSUFFICIENT_PRIVILEGE)
				stringBuilder.append("Insufficient Privilege");
			stringBuilder.append("\"}");
			return stringBuilder.toString();
		}
	}

	@RequestMapping(value = "/admin/ajax/check_designation_name", method = RequestMethod.POST)
	@ResponseBody
	public String checkDepartmentName(String except, String dsgnName) {
		if (designationDAO.ifExistsDesignation(except, dsgnName)) {
			return "{\"exists\":true}";
		} else {
			return "{\"exists\":false}";
		}
	}

	@RequestMapping(value = "/admin/delete_designation", method = RequestMethod.POST)
	@ResponseBody
	public String deleteDepartment(int id) {
		int errorCode = designationService.deleteDesignation(login.getUsername(), id);
		String status;
		switch (errorCode) {
			case SUCCESS:
				status = "ok";
				break;
			case ERROR:
				status = "error";
				break;
			case INSUFFICIENT_PRIVILEGE:
				status = "error";
				break;
			case USER_IN_DESIGNATION:
				status = "error";
				break;
			default:
				status = "error";
		}
		return "{\"status\":\"" + status + "\",\"errorCode\":" + errorCode + "}";
	}

	@RequestMapping(value = "/admin/delete_users_in_dsgn", method = RequestMethod.POST)
	@ResponseBody
	public String deleteUsersInDept(int id) {
		int i = designationService.deleteUsersInDesignation(login.getUsername(), id);
		String status;
		String errorMessage = "";
		switch (i) {
			case SUCCESS:
				status = "ok";
				break;
			case ERROR:
				status = "error";
				errorMessage = "Internal Error";
				break;
			case INSUFFICIENT_PRIVILEGE:
				status = "error";
				errorMessage = "Insufficient Privilege";
				break;
			default:
				status = "error";
				;
				errorMessage = "Internal Error";
		}
		return "{\"status\":\"" + status + "\",\"errorMessage\":\"" + errorMessage + "\"}";
	}

	@RequestMapping(value = "/admin/modify_designation", method = RequestMethod.GET)
	public String getModifyDepartment(@RequestParam("dsgnId") int dsgnId, ModelMap map) {
		DesignationEntity designation = designationService.getDesignation(login.getUsername(), dsgnId);
		map.addAttribute("dsgn", designation);
		if (designation == null) {
			map.addAttribute("errorMessage",
					"Failed to fetch designation #" + dsgnId + ".");
		}
		return "admin/modify_designation";
	}

	@RequestMapping(value = "/admin/modify_designation", method = RequestMethod.POST)
	@ResponseBody
	public String postModifyDepartment(DesignationEntity dsgn) {
		switch (designationService.updateDesignation(login.getUsername(), dsgn)) {
			case SUCCESS:
				return "{\"status\":\"success\"}";
			case ERROR:
				return "{\"status\":\"fail\",\"errorMessage\":\"Internal Error\"}";
			case INSUFFICIENT_PRIVILEGE:
				return "{\"status\":\"fail\",\"errorMessage\":\"Insufficient Privilege\"}";
			default:
				return "{\"status\":\"fail\",\"errorMessage\":\"Internal Error\"}";
		}
	}


}
